const api = require("../../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    myLatitude:null,
    myLongitude:null,
    listData:[],
    pageNo:1
  },
  _loadListData(){
    if(this.data.pageNo > 0){
        const _self = this;
        api.post(api.url.bxscnBatteryType,{
            pageNo:this.data.pageNo
        },res=>{
            console.log(res)
            if(res.data.code==200){
                let deviceList = res.data.body.items||[];
                _self.setData({
                    listData:_self.data.listData.concat(deviceList),
                    pageNo:_self.data.pageNo + 1
                })
            }else if(res.data.code==202){
                _self.setData({
                    pageNo:-1
                })
            }else if(res.data.msg){
                my.showModal({
                  content: res.data.msg,
                  showCancel:false
                })
            }else{
                my.showToast({
                  content: '获取数据失败',
                  icon:'error'
                })
            }
        })
    }
  },

  btnScan(){
      const self = this;
      my.scanCode({
          success(res){
              const qrcode = res.result.split('/');
              if(qrcode[4]=="coupon_activity"&&parseInt(qrcode[5])>0){
                  //优惠券二维码
                  api.post(api.url.couponInfo,{
                      activityId:qrcode[5]
                  },res=>{
                      console.log(res.data.body)
                      if(res.data.code==200){
                          self.setData({
                              couponInfo:res.data.body,
                              modalName:'coupon'
                          })
                      }else{
                          my.showModal({
                            title: '温馨提示',
                            content: res.data.msg||'优惠券异常！',
                            showCancel:false,
                          })
                      }
                  })
              }else{
                  my.showModal({
                    title: '温馨提示',
                    content: '请扫正确的二维码',
                    showCancel:false,
                  })
              }
          }
      })
  },

  toMine:function(e){
      my.navigateTo({
        url: '/pages/mine/mine',
      })
  },

  toIndex:function(e){
      my.reLaunch({
        url: '/pages/map/index'
      });
  },

  toShop(){
      const info = app.globalData.shopInfo;
      my.navigateToMiniProgram({
          appId:info.appId,
          path:info.path,
          envVersion:info.envVersion
      })
  },

  toDeviceInfo:function(e){
      let id = e.currentTarget.id;
      let latitude = this.data.myLatitude || '';
      let longitude = this.data.myLongitude || '';
      my.navigateTo({
        url: '../info/info?id='+id+'&latitude='+latitude+"&longitude="+longitude
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.setData({
          myLatitude:options.latitude,
          myLongitude:options.longitude
      })
      this._loadListData();
  },
});
